package com.wbdgj.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.internal.LinkedTreeMap;
import com.wbdgj.R;
import com.wbdgj.http.HttpAdapter;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * ListAdapter
 * Created by XiaoWei on 2015-11-14.
 */
public class WbAdapter extends BaseListAdapter<LinkedTreeMap> {
    private int[] imgId = {R.mipmap.md_001, R.mipmap.md_002, R.mipmap.md_003, R.mipmap.md_004, R.mipmap.md_005, R.mipmap.md_006, R.mipmap.md_007, R.mipmap.md_008};
    public WbAdapter(Context context, List<LinkedTreeMap> objects) {
        super(context, objects);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            if (getItemViewType(position) == 0) {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.layout_has_no_data, parent, false);
                holder.noDataRootLayout = (LinearLayout) convertView.findViewById(R.id.root_layout);
            } else {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.home_qltj_item, parent, false);
                holder.name = (TextView) convertView.findViewById(R.id.name);
                holder.money = (TextView) convertView.findViewById(R.id.money);
                holder.address = (TextView) convertView.findViewById(R.id.address);
                holder.jl = (TextView) convertView.findViewById(R.id.jl);
                holder.ls = (TextView) convertView.findViewById(R.id.ls);
                holder.ls2 = (TextView) convertView.findViewById(R.id.ls2);
                holder.ls3 = (TextView) convertView.findViewById(R.id.ls3);
                holder.mdName = (TextView) convertView.findViewById(R.id.mdName);
                holder.image = (ImageView) convertView.findViewById(R.id.image);
                holder.mdLay = (LinearLayout) convertView.findViewById(R.id.mdLay);
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (hasNoData) {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(getScreenWidth(),getScreenHeight()*2/3);
            holder.noDataRootLayout.setLayoutParams(lp);
        } else {
            final DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
            holder.name.setText(mDataList.get(position).get("NAME")+"");
            holder.mdName.setText(mDataList.get(position).get("NAME")+"");
            holder.address.setText(mDataList.get(position).get("ADDRESS")+"");
            if (mDataList.get(position).containsKey("BRANCH_AVG")){
                holder.money.setVisibility(View.VISIBLE);
                holder.money.setText("人均"+decimalFormat.format(mDataList.get(position).get("BRANCH_AVG"))+"元/小时");
            }else{
                holder.money.setVisibility(View.GONE);
            }
            if (mDataList.get(position).containsKey("DISTANCE")){
                holder.jl.setVisibility(View.VISIBLE);
                if (decimalFormat.format(mDataList.get(position).get("DISTANCE")).length()>3){
                    holder.jl.setText(">"+Math.round(Integer.parseInt(decimalFormat.format(mDataList.get(position).get("DISTANCE")))/100d)/ 10d+"KM");
                }else{
                    holder.jl.setText(">"+decimalFormat.format(mDataList.get(position).get("DISTANCE"))+"M");
                }
            }else{
                holder.jl.setVisibility(View.GONE);
            }

            if (mDataList.get(position).containsKey("TAB")){
                String[] split = (mDataList.get(position).get("TAB")+"").split(",");
                if (split.length == 1){
                    holder.ls.setVisibility(View.VISIBLE);
                    holder.ls2.setVisibility(View.GONE);
                    holder.ls3.setVisibility(View.GONE);
                    holder.ls.setText(split[0]);
                }
                if (split.length == 2){
                    holder.ls.setVisibility(View.VISIBLE);
                    holder.ls2.setVisibility(View.VISIBLE);
                    holder.ls3.setVisibility(View.GONE);
                    holder.ls.setText(split[0]);
                    holder.ls2.setText(split[1]);
                }
                if (split.length == 3){
                    holder.ls.setVisibility(View.VISIBLE);
                    holder.ls2.setVisibility(View.VISIBLE);
                    holder.ls3.setVisibility(View.VISIBLE);
                    holder.ls.setText(split[0]);
                    holder.ls2.setText(split[1]);
                    holder.ls3.setText(split[2]);
                }
            }else{
                holder.ls.setVisibility(View.VISIBLE);
                holder.ls.setText("单体门店");
                holder.ls2.setVisibility(View.GONE);
                holder.ls3.setVisibility(View.GONE);
            }


            if (!(mDataList.get(position).get("IMG_URL_ONE")+"").equals("null")){
                holder.image.setVisibility(View.VISIBLE);
                holder.mdLay.setVisibility(View.GONE);
                Glide.with(mContext).load(HttpAdapter.HEAD_URL+mDataList.get(position).get("IMG_URL_ONE")).placeholder(R.mipmap.wbtp_wait).error(R.mipmap.wbtp_error).into(holder.image);
            }else{
                Glide.with(mContext).load("").placeholder(R.mipmap.wbtp_wait).error(R.mipmap.wbtp_error).into(holder.image);
                holder.image.setVisibility(View.GONE);
                holder.mdLay.setVisibility(View.VISIBLE);
                int index = (int) (Math.random() * imgId.length);
                holder.mdLay.setBackgroundResource(imgId[index]);
            }
        }
        return convertView;
    }

    private static final class ViewHolder {
        TextView name, money, address, jl, ls, mdName, ls2, ls3;
        ImageView image;
        LinearLayout noDataRootLayout, mdLay;
    }

    private int getScreenWidth() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.widthPixels;
    }

    private int getScreenHeight() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.heightPixels;
    }

}
